package com.sgb.contract.view.ui.details;

import android.content.Intent;
import android.graphics.Typeface;
import android.os.Build;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.StyleSpan;
import android.view.View;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.google.android.material.tabs.TabLayout;
import com.sgb.contract.BR;
import com.sgb.contract.R;
import com.sgb.contract.base.BaseActivity;
import com.sgb.contract.databinding.ContractActivityOtherInfoBinding;
import com.sgb.contract.model.ApprovalLogBean;
import com.sgb.contract.model.UpdateBean;
import com.sgb.contract.utils.ArouterConfigs;
import com.sgb.contract.view.ui.fragment.ApprovalLogFragment;
import com.sgb.contract.view.ui.fragment.BasicInfoFragment;
import com.sgb.contract.view.ui.fragment.OperationRecordsFragment;
import com.sgb.contract.view.ui.fragment.UpdateFragment;
import com.sgb.contract.view.widget.pop.DataDetailedPop;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.RequiresApi;
import androidx.core.util.Pair;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;

/**
 * 其他信息
 *
 * @author cyj
 */
@Route(path = ArouterConfigs.AR_CONTRACT_OTHER_INFO)
public class OtherInfoActivity extends BaseActivity<ContractActivityOtherInfoBinding, OtherInfoViewModel> {

    private List<ApprovalLogBean> approvalLogList;
    private DataDetailedPop mDataDetailedPop;

    @Override
    protected int getLayoutResId() {
        return R.layout.contract_activity_other_info;
    }


    @Override
    protected void initViewModel() {
        mViewModel = createViewModel(OtherInfoViewModel.class, BR.mOtherInfoViewModel);
    }

    @Override
    protected void init() {
        mDataBinding.head.title.setText(R.string.other_info_contract_detail);
        Intent intent = getIntent();
        if (intent != null) {
            String auditId = intent.getStringExtra("auditId");
            if (!TextUtils.isEmpty(auditId)) {
                mViewModel.getContractApprovalLogList(auditId);
            } else {
                initTabView();
            }
        }

        mDataDetailedPop = new DataDetailedPop(this);
        mDataDetailedPop.setBgView(mDataBinding.grayLayout);
    }

    @Override
    protected void initObserve() {
        mViewModel.approvalLogBean.observe(this, approvalLogBeanList -> {
            approvalLogList = approvalLogBeanList;
            initTabView();
        });
    }

    private void initTabView() {
        // TabLayout.Tab初始化
        String status = getIntent().getStringExtra("status");
        List<Pair<String, Fragment>> mList = new ArrayList<>();
        mList.add(new Pair<>("基本信息", new BasicInfoFragment()));
        mList.add(new Pair<>("操作记录", new OperationRecordsFragment()));
        if (!status.contains("1")) {
            mList.add(new Pair<>("审批日志", new ApprovalLogFragment(approvalLogList)));
            mList.add(new Pair<>("修改记录", new UpdateFragment()));
        }
        ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager(), mList);
        mDataBinding.vp.setAdapter(viewPagerAdapter);
        mDataBinding.vp.setOffscreenPageLimit(mList.size());
        mDataBinding.tabGroup.setupWithViewPager(mDataBinding.vp);
        mDataBinding.vp.setCurrentItem(0);
        mDataBinding.tabGroup.getTabAt(0).select();
    }

    @Override
    protected void initListener() {
        mDataBinding.head.ivBack.setOnClickListener(v -> finish());
        mDataBinding.tabGroup.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                if (tab==null|| tab.getText()==null){
                    return;
                }
                String trim = tab.getText().toString().trim();
                SpannableString spannableString = new SpannableString(trim);
                StyleSpan styleSpan = new StyleSpan(Typeface.BOLD);
                spannableString.setSpan(styleSpan,0,trim.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
                tab.setText(spannableString);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                if (tab==null|| tab.getText()==null){
                    return;
                }
                String trim = tab.getText().toString().trim();
                SpannableString spannableString = new SpannableString(trim);
                StyleSpan styleSpan = new StyleSpan(Typeface.NORMAL);
                spannableString.setSpan(styleSpan,0,trim.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
                tab.setText(spannableString);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

    }


    /**
     * 设置TabView选中与未选中的UI样式
     *
     * @param tab      tab.view.getChildAt(1)是TextView，不会保证未来是否会变更位置
     * @param isSelect 是否选中
     */
    private void setTabViewColor(TabLayout.Tab tab, boolean isSelect) {
        View view = tab.view.getChildAt(1);
        if (view instanceof TextView) {
            ((TextView) view).setTextColor(getResources().getColor(isSelect ? R.color.color_ffef4033 : R.color.color_ff333333));
            ((TextView) view).setTypeface(Typeface.defaultFromStyle(isSelect ? Typeface.BOLD : Typeface.NORMAL));
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    public void show(RecyclerView rv, UpdateBean bean) {
        mDataDetailedPop.show(rv, bean);
    }
}
